<?php

namespace app\api\controller\v2;

use app\common\service\agent\SettingService;
use app\model\AgentChannel;
use app\model\AgentSupplier;
use app\model\ChannelServDiscount;
use app\model\TakeoutSlider;
use app\model\Template;
use app\model\TemplateService;
use EasyWeChat\Factory;
use think\facade\Db;
use think\facade\Log;
use think\Request;
use app\common\model\AgentServiceConfig;

/**
 * 获取配置
 */
class Config extends \app\BaseController
{
    protected $middleware = [
        \app\middleware\ApiChannel::class,
        \app\middleware\ApiAgentConfig::class,
    ];

    /**
     * 基本配置【无需登录】
     */
    public function basic(Request $request)
    {
        $url = input('url');

        $template_url = input('template_url');

        $channel_id = $request->agentChannel['id'];
        //判断是否存在供应商配置
        $agent_id = $request->agentChannel['agent_id'];

        $host = env('app.api_url');

        if (strstr($url,'#')){
            $url = str_replace('#','ymhcccc',$url);//有美惠充冲冲冲  兼容快站
        }

        // 微信公众号授权回调地址
        $redirectUri = $host . '/api/v2.wechat/authRedirect?url=' . urlencode($url);

        if (input('channel') == AgentChannel::YMHC_ADMIN_CHANNEL) {
            $channelServ = TemplateService::getAllServType();
        } else {
            $channelServ = (new ChannelServDiscount())->getRealServ($channel_id, $agent_id,$template_url);
        }
        return success('操作成功', [
            'appid' => $request->agentConfig['app_id'],
            'redirect_uri' => $redirectUri,
            'channel_serv' => array_values($channelServ),
        ]);
    }


    /**
     * 该代理商已开通的产品
     */
    public function productList(Request $request)
    {
        if (input('channel') == AgentChannel::YMHC_ADMIN_CHANNEL) {

            $list = TemplateService::getAllServType();

        } else {

            $agent_id = $request->agentChannel['agent_id'];

            $list = (new AgentServiceConfig())->productList($agent_id);
        }
        return success('操作成功', $list);

    }

    /**
     * jssdk.
     */
    public function jssdk(Request $request)
    {
//        Log::info('获取到的Appid：'.$request->agentConfig['app_id']);
//        Log::info('获取到的秘钥：'.$request->agentConfig['app_secret']);

        $app = Factory::officialAccount([
            'app_id' => $request->agentConfig['app_id'],
            'secret' => $request->agentConfig['app_secret'],

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        ]);
        $url = input('post.url');
        $jsApiList = input('post.jsApiList', []);
        $openTagList = input('post.openTagList', []);
        $debug = false;
        $res = $app->jssdk->setUrl($url)->buildConfig($jsApiList, $debug, false, false,$openTagList);

        return success('操作成功', $res);
    }

    /**
     * 获取客服链接.
     */
    public function getCustomerServiceUrl(Request $request)
    {
        $url = Db::name('agent_config')
            ->where('agent_id', $request->agentChannel['agent_id'])
            ->value('customer_service_url');
        // if (empty($url)) {
        //     $url = 'https://tb.53kf.com/code/client/47906e25cefde3aaf82ad360af29d8e47/3';
        // }

        return success('操作成功', [
            'url' => $url,
        ]);
    }

    /**
     * 获取轮播图和九宫格
     */
    public function getGroupTemplateConfig(Request $request){
        $agent_id = $request->agentChannel['agent_id'];
        $type = input('type',TakeoutSlider::TYPE_TACKOUT);
        $config = (new SettingService())->getSliderAndVajra($agent_id,$type);
        return success('操作成功',$config);
    }

}
